{include file="public/header" /}
<link rel="stylesheet" href="/static/index/css/page/classify.css">
<body>
<div class="build-navbar" data-title="分类" data-left="false"></div>
<div class="main mt-88">
    {if $grade == 2}
    <div class="top-cate scroll-x">
        <div class="category-container cate-1"></div>
    </div>
    {/if}
    <div class="bottom-cate flex justify-between">
        <div class="scroll-container theme-color">
            <div class="left flex flex-col {$grade == 1?'cate-1':'cate-2'}"></div>
        </div>
        <div id="cateTopFix" class="right flex-1 px-30 scroll-container product-tpl-list"></div>
    </div>

</div>
<script type="text/html" id="cate-tpl-1">
    {{#  layui.each(d, function(index, item){ }}
    <div data-id="{{item.id}}" lay-change="cate_1"
         class="category-item flex flex-col items-center justify-center {{#  if(!index){ }}lay-this{{#  } }}"
         lay-url=""
         lay-event="jump">
        <img src="{{item.icon}}" alt="{{item.title}}">
        <div class="category-text">{{item.title}}</div>
    </div>
    {{#  }); }}
</script>
<script type="text/html" id="cate-tpl-2">
    {{#  layui.each(d, function(index, item){ }}
    <div data-id="{{item.id}}" lay-change="cate_2" class="item {{#  if(!index){ }}lay-this{{#  } }}">{{item.title}}
    </div>
    {{#  }); }}
    {{#  if(d.length === 0){ }}
    <div class="item ">暂无数据</div>
    {{#  } }}
</script>


<script type="text/html" id="product-tpl">
    {{# layui.each(d.data, function(index, item){ }}
    <div class="pro-box flex items-center justify-between py-30"
         lay-url="{:url('product/details')}?id={{item.id }}"
         lay-event="jump">
        <div class="icon"><img lay-src="{{= item.icon }}" alt=""></div>
        <div class="info flex flex-col ">
            <div class="title">{{= item.title }}</div>
            <div class="underline">￥{{= item.price }}</div>
            <div class="price front">{{= item.vip_price }}</div>
        </div>
        <img src="/static/index/images/classify/cart.png" alt="" class="cart">
    </div>

    {{# }); }}
</script>

{include file="public/menu" select="2"/}
</body>
{include file="public/footer" /}
<script>
    layui.use(['methods', 'listen'], function () {
        var methods = layui.methods,
            listen = layui.listen,
            a = layui.jquery,
            layTpl = layui.laytpl,
            grade = parseInt('{$grade}'),
            api = {getCate: '{:url("classify/getCate")}', getProduct: '{:url("product/getList")}'};

        listen.initTopFixBar('cateTopFix');

        getCate()

        methods.event.change(['cate_1', 'cate_2'], function (data, that) {
            grade === 2 ? getCate(data.id, 2) : getProduct(data.id);
        }, function (data, that) {
            getProduct(data.id)
        });

        function getCate(pid = 0, type = 1) {
            methods.fetchGet(api.getCate, {pid: pid}, function (d) {
                var i = grade === 1 ? '2' : type;
                layTpl(a('#cate-tpl-' + i).html()).render(d.data, function (html) {
                    a('.cate-' + type).html(html);
                });
                if (d.data.length > 0) {
                    grade === 2 && type === 1 ? getCate(d.data[0].id, 2) : getProduct(d.data[0].id);
                } else {
                    a('.product-tpl-list').html('');
                }
            }, false);
        }

        function getProduct(id) {
            methods.getFlowData(api.getProduct + '?' + 'cate_' + grade + '=' + id, {
                elem: 'product-tpl-list',
                tpl: 'product-tpl',
                scrollElem: a('.right')
            });
        }


    });
</script>
</html>